package com.nasarius.bank.scorecard.web.controller.user;

import java.io.IOException;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nasarius.bank.scorecard.dao.BankFactorsActualDAO;
import com.nasarius.bank.scorecard.dao.UploadLogDAO;
import com.nasarius.bank.scorecard.domain.BankFactorsActual;
import com.nasarius.bank.scorecard.domain.UploadLog;
import com.nasarius.bank.scorecard.util.CSVUtil;
import com.nasarius.bank.vo.UploadItemVO;

@Controller
public class UploadController {
	@Autowired
	private BankFactorsActualDAO bankFactorsActualDAO;
	@Autowired	
	private UploadLogDAO uploadLogDAO;
	public void setUploadLogDAO(UploadLogDAO uploadLogDAO) {
		this.uploadLogDAO = uploadLogDAO;
	}

	public void setBankFactorsActualDAO(
			BankFactorsActualDAO bankFactorsActualDAO) {
		this.bankFactorsActualDAO = bankFactorsActualDAO;
	}

	@RequestMapping(value = "/uploadCSV/select", method = RequestMethod.GET)
	public void getUploadForm(Model model) {
		model.addAttribute(new UploadItemVO());
	}

	@RequestMapping(value = "/uploadCSV/upload", method = RequestMethod.POST)
	public void upload(UploadItemVO uploadItem, BindingResult result)  throws Exception{
		uploadLog("Start", uploadItem.getFileData().getOriginalFilename()+" has been picked up for processing");
		ArrayList<BankFactorsActual> bankFactorsActualList = null;
		if (result.hasErrors()) {
			uploadLog("Failed", "Invalid CSV Input File");
			for (ObjectError error : result.getAllErrors()) {
				uploadLog("Technical Error", error.getCode()+"");
				System.err.println("Error: " + error.getCode() + " - "
						+ error.getDefaultMessage());
			}

			
			// return "/uploadCSV/upload";
		}
		try {
			uploadLog("Uploading", "CSV file is being Processed");
			bankFactorsActualList = CSVUtil.readCSV(uploadItem
					.getFileData().getInputStream());
		} catch (IOException e) {
			uploadLog("Failed", "CSV file upload failed");
			e.printStackTrace();
			throw e;
		}

		// Some type of file processing...
		System.err.println("-------------------------------------------");
		System.err.println("Test upload: " + uploadItem.getName());
		System.err.println("Test upload: "
				+ uploadItem.getFileData().getOriginalFilename());
		System.err.println("-------------------------------------------");
		if (bankFactorsActualList != null)
			for (BankFactorsActual bankFactorsActual : bankFactorsActualList) {
				bankFactorsActualDAO.insertBankFactorsActual(bankFactorsActual);
			}
		uploadLog("Success", "CSV File Upload Completed Sucessfully");
		// return "/uploadCSV/result";
	}
	
	private void uploadLog(String status, String message){
		UploadLog uploadLog = new UploadLog();
		uploadLog.setMessage(message);
		uploadLog.setStatus(status);
		uploadLog.setUser(SecurityContextHolder.getContext().getAuthentication().getName());
		uploadLogDAO.insertUploadLog(uploadLog);
	}

}
